package com.smartao.core.license;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class License implements Serializable {

	private static final long serialVersionUID = -5512227095443134292L;
	private static final Logger logger = LoggerFactory.getLogger(License.class);
	
	private String productName;
	private String productVersion;
	private String serverID;
	private String organization;
	private int maxLicense;
	private String expireDate;
	private LicenseType licenseType;
	private String signature;

	private boolean verified = true;

	public License() {}

	public License(LicenseRegistry licenseRegistry) {
		this.expireDate = licenseRegistry.getExpireDate();
		this.licenseType = licenseRegistry.getLicenseType();
		this.maxLicense = licenseRegistry.getMaxLicense();
		this.organization = licenseRegistry.getOrganization();
		this.productName = licenseRegistry.getProductName();
		this.productVersion = licenseRegistry.getProductVersion();
		this.serverID = licenseRegistry.getServerID();
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer("<license>");
		buffer.append("<productName>").append(productName).append("</productName>");
		buffer.append("<productVersion>").append(productVersion).append("</productVersion>");
		buffer.append("<serverID>").append(serverID).append("</serverID>");
		buffer.append("<organization>").append(organization).append("</organization>");
		buffer.append("<maxLicense>").append(maxLicense).append("</maxLicense>");
		buffer.append("<expireDate>").append(expireDate).append("</expireDate>");
		buffer.append("<licenseType>").append(licenseType).append("</licenseType>");
		buffer.append("<signature>").append(signature).append("</signature>");
		buffer.append("</license>");
		return buffer.toString();
	}

	public String getVerifyText() {
		StringBuffer buffer = new StringBuffer("<license>");
		buffer.append("<productName>").append(productName).append("</productName>");
		buffer.append("<productVersion>").append(productVersion).append("</productVersion>");
		buffer.append("<serverID>").append(serverID).append("</serverID>");
		buffer.append("<organization>").append(organization).append("</organization>");
		buffer.append("<maxLicense>").append(maxLicense).append("</maxLicense>");
		buffer.append("<expireDate>").append(expireDate).append("</expireDate>");
		buffer.append("<licenseType>").append(licenseType).append("</licenseType>");
		buffer.append("</license>");
		return buffer.toString();
	}

	public String toBase64String() {
		String licensexml = this.toString();
		Base64 base64 = new Base64();
		String base64License = null;
		try {
			base64License = new String(base64.encode(licensexml.getBytes("utf-8")));
		} catch (Exception e) {
			logger.error("Encode license failed!", e);
		}
		return base64License;
	}

	/**
	 * license是否过期
	 */
	public boolean isExpired() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date expDate = format.parse(this.getExpireDate());
			return expDate.before(new Date());
		} catch (ParseException e) {
			return true;
		}
	}

	/**
	 * 是否是演示版
	 */
	public boolean isEvaluation() {
		return licenseType.equals(LicenseType.Evaluation);
	}

	/**
	 * 是否达到license用户最大限制，maxLicense为-1表示无用户限制
	 */
	public boolean userLimit(int number) {
		if (maxLicense == -1) {
			return false;
		}
		return number <= maxLicense;
	}

	public boolean isAvailable(int number) {
		return !isExpired() && !isEvaluation() && !userLimit(number);
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String getProductName() {
		return productName;
	}

	public String getProductVersion() {
		return productVersion;
	}

	public String getServerID() {
		return serverID;
	}

	public String getOrganization() {
		return organization;
	}

	public int getMaxLicense() {
		return maxLicense;
	}

	public String getExpireDate() {
		return expireDate;
	}

	public LicenseType getLicenseType() {
		return licenseType;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public void setProductVersion(String productVersion) {
		this.productVersion = productVersion;
	}

	public void setServerID(String serverID) {
		this.serverID = serverID;
	}

	public void setOrganization(String organization) {
		this.organization = organization;
	}

	public void setMaxLicense(int maxLicense) {
		this.maxLicense = maxLicense;
	}

	public void setExpireDate(String expireDate) {
		this.expireDate = expireDate;
	}

	public void setLicenseType(LicenseType licenseType) {
		this.licenseType = licenseType;
	}

	public boolean isVerified() {
		return verified;
	}

	public void setVerified(boolean verified) {
		this.verified = verified;
	}
	
	public void touch() {
		logger.info("LicenseType:{}",licenseType);
		logger.info("Organization:{}",organization);
		logger.info("MaxLicense:{}",maxLicense);
		logger.info("ExpireDate:{}",expireDate);
		logger.info("Verified:{}",verified);
	}
}
